<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
include_once("../config.inc.php");

session_start();
$objMemberInfo = $_SESSION["Member_Info"];
if(time() - $objMemberInfo -> WagerCommonTime <= 1){
	sleep(1);
}
$_SESSION["Member_Info"] -> WagerCommonTime = time();
if($objMemberInfo -> isLock == '2'){
	$GLOBALS["errormessage"] = $_strStopWager;
	include("wager_error.php");
}

$_LangPath = "wagercommon";	//語言檔參數，務必放在最前面
include_once(__Member_Path."/logincheck.php");
include_once(__Language_Path."/selectlanguage_member.php");
include_once(__Modules_Path."/Wager.lib.php");
include_once(__Modules_Path."/WagerForm.lib.php");
include_once(__Modules_Path."/joinnumber.lib.php");
include_once(__Common_Path."/Lotto.inc.php");

$intLID = (int)$_data["LottoID"];
$intGID = (int)$_data["GambleID"];
$intLType = (int)$_arrLotto[$intLID]["LType"];
$intBT = (int)$_data["Bet_Type"];
$arrLotto = $_arrLotto[$intLID];
$arrChoiceNumber = $_data["ChoiceNumber"];
$strChoiceNumber = "";
$intOddsID = 0;
$flOdds = 0;
$strSType = trim($_data["sType"]);

$arrNum = array();
$isChoiceNumber = false;
if($strSType == "o"){
	if(is_array($arrChoiceNumber)){
		foreach($arrChoiceNumber as $intNum){	//千位
			$arrNum[] = (int)$intNum;
		}
	}

}else{
	if(is_array($arrChoiceNumber)){
		if($intLType == 1){
			$arrMinNo = array(31 => 5, 32 => 2, 33 => 3, 34 => 4, 35 => 2, 36 => 3, 37 => 4, 38 => 2, 39 => 3, 40 => 4, 41 => 2, 42 => 3, 43 => 4);
			$arrNum = array();
			$arrOdds = array();
			$arrCNumber = array();
			$i = 1;
			foreach($arrChoiceNumber[$intBT] as $intNum => $strVal){
				$arrV = explode("#", $strVal);
				$arrNum[$i] = (int)$intNum;
				$arrOdds[$arrNum[$i]] = $arrV[1];
				$i++;
			}
			if(sizeof($arrNum) < $arrMinNo[$intBT]){
				$Err_Message = "No Wager Number Data!!";
				include("wager_error.php");
			}
			$arrTmp = combination($arrNum, $arrMinNo[$intBT], 1, array(), null, null);
			$arrNum = array();
			foreach($arrTmp as $strV){
				$arrV = explode(".", $strV);
				sort($arrV);
				$arrCNumber[] = $arrV;
				$intMinOddsNum = 0;
				$flMinOdds = 9999999;
				foreach($arrV as $intNum){
					if($arrOdds[$intNum] < $flMinOdds){
						$intMinOddsNum = $intNum;
						$flMinOdds = $arrOdds[$intNum];
					}
				}
				$arrNum[] = $intMinOddsNum;
			}
			$isChoiceNumber = true;

		}elseif($intLType == 3){ //if($intLType == 1){
			switch($intBT){
				case 11:
					foreach($arrChoiceNumber[4] as $intMilli){	//千位
						foreach($arrChoiceNumber[3] as $intCent){	//百位
							foreach($arrChoiceNumber[2] as $intDec){	//十位
								foreach($arrChoiceNumber[1] as $intUnit){	//個位
									$arrNum[] = $intMilli*1000+$intCent*100+$intDec*10+$intUnit*1;
								}
							}
						}
					}
					break;

				case 1:
				case 2:
				case 3:
				case 4:
				case 5:
				case 6:
				case 12:
					foreach($arrChoiceNumber[2] as $intDec){	//十位
						foreach($arrChoiceNumber[1] as $intUnit){	//個位
							$intNum = $intDec*10+$intUnit*1;
							if($intBT == 12){
								if(floor($intNum / 10) > ($intNum % 10))continue;
							}
							$arrNum[] = $intNum;
						}
					}
					break;

				case 7:
				case 8:
				case 9:
				case 10:
				case 13:
					foreach($arrChoiceNumber[3] as $intCent){	//百位
						foreach($arrChoiceNumber[2] as $intDec){	//十位
							foreach($arrChoiceNumber[1] as $intUnit){	//個位
								$intNum = $intCent*100+$intDec*10+$intUnit*1;
								if($intBT == 13){
									if($intNum < 100){
										if(floor($intNum / 10) > ($intNum % 10))continue;
									}else{
										if(floor($intNum / 10) > ($intNum% 100))continue;
										if(floor(($intNum % 100) / 10) > ($intNum % 10))continue;
									}
								}
								$arrNum[] = $intNum;
							}
						}
					}
					break;
			} //switch($intBT){
		} //if($intLType == 3){
	} //if(is_array($arrChoiceNumber)){
} //if($strSType == "o"){

if(sizeof($arrNum) == 0){
	$Err_Message = "No Wager Number Data!!";
	include("wager_error.php");
}

include_once(__Common_Path."/betparam/BetParam_".$intLID.".inc.php");
include_once(__Common_Path."/limit/LottoLimit_".$intLID.".inc.php");
include_once(__Common_Path."/ExchangeRate.inc.php");
include_once(__Modules_Path."/RealBet.lib.php");
include_once(__Common_Path."/gambletype/GambleType".$objMemberInfo -> GambleType."_".$intLID.".inc.php");//加進分盤資料

if(__isConnectToOtherWeb == 1){
	$flMemERate = (empty($objMemberInfo -> ExRate))?1:$objMemberInfo -> ExRate;
}else{
	$flMemERate = (empty($_ECRate[$objMemberInfo -> ERID]["Rate"]))?1:$_ECRate[$objMemberInfo -> ERID]["Rate"];
}
$intApointNum = $_arrGLineNumToApointNum[$intLID][$intBT][$intNum];
if($intApointNum == null)$intApointNum = 0;

if(!is_array($_arrBetParam[$intBT])){
	$Err_Message = "No Wager Relation Data!!";
	include("wager_error.php");
}

if($intLType == 3)include_once(__Common_Path."/CommToOddsRate_".$intLID.".inc.php");
include_once(__Modules_Path."/Commission.lib.php");
//找出 使用者 的賠率兌換，傳回陣列
$arrUserOPlus = getUserCommToOdds($objMemberInfo -> ID, $intLID, array($intBT));
if($arrUserOPlus === false)ErrMsgOutputHTML("Error", "", 1);

$intMaxUsedNumGold = 0;	//選擇號碼中，已下注最大金額
$flMaxOdds = 0; //選擇號碼中，最大賠率
$arrWagerCon = array();
$intTotalWNo = 0;
$arrApNum = array();
//取得最新號碼賠率資料
foreach($arrNum as $k => $intNum){
	$intApNum = (int)$_arrGLineNumToApointNum[$intLID][$intBT][$intNum];
	$arrApNum[$intApNum] = $intApNum;
	$flOddsPlus = $arrUserOPlus[$intBT][$intApNum][11] * $_arrChRate[$intBT][$intApNum][$objMemberInfo -> GambleType+10];

	$strChoiceNumber = (is_array($arrCNumber[$k]))?implode(",", $arrCNumber[$k]):"";

	$arrGameDate = getWagerArrayData($intLID, $intLType, $intGID, $intBT, $intNum, $intOddsID, $flOdds, $strChoiceNumber);
	if($arrGameDate["isError"]){
		$strBTTip = getWagerConditionsForError($intLType, $intLID, $intBT, $intNum);
		$Err_Message = $_strWagerCommon[$arrGameDate["Err_Message"]]."<br />".$strBTTip;
		include("wager_error.php");
	}
	$flOdds = floatval($arrGameDate["Odds"]) + $_arrGambleType[$intBT][$intApointNum] + $flOddsPlus;
	if($isChoiceNumber){
		$arrWagerCon[$k] = getWagerConditions($intLType, $intBT, $intNum, $flOdds, $arrGameDate["SerialNo"], 0, $arrCNumber[$k], 1);
	}else{
		$arrWagerCon[$k] = getWagerConditions($intLType, $intBT, $intNum, $flOdds, $arrGameDate["SerialNo"]);
	}
	if($intTotalWNo != 0)$arrWagerCon[$k] = preg_replace("/.+<br \/>/", "", $arrWagerCon[$k]);
	$intTotalWNo++;

	$arrFrmWagerCon[$k] = $intNum."#".$arrGameDate["OddsID"]."#00000#".$intBT."#".$strChoiceNumber;
	$intTmp = $_arrMemStake[$intBT][$intGID][$intNum];
	if($intMaxUsedNumGold < $intTmp)$intMaxUsedNumGold = $intTmp;
	if($flMaxOdds < $flOdds)$flMaxOdds = $flOdds;

}
$strWagerConditions = WagerFormTrans($intLID, $intBT, join("<br />", $arrWagerCon));
//print_r($arrChoiceNumber);
//是否上層停押
/*$strParentID = $objMemberInfo -> SubID.",".$objMemberInfo -> MainID.",".$objMemberInfo -> StockID;
if(!$strTop = GetOneStopWagerBet($intGID, $intBT, $intNum, $strParentID)){
	$Err_Message = $GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"];
	include("wager_error.php");
}

if($strTop == "Stop"){
	$Err_Message = $_strWagerCommon["StopWager"];
	include("wager_error.php");
}*/

include_once(__Root_Path."/tmp/credit-".$objMemberInfo -> ID.".php");


$fp = @file(__Membertemplate_Path."/number92_qwager.html");
$fp = implode("", $fp);
$fp = setHTMLCharset($fp);
$fp = str_replace("{sid}", $_data["sid"], $fp);
$fp = str_replace("{ShowAccount}", $objMemberInfo -> Account, $fp);
$fp = str_replace("{ShowCredit}", $objMemberInfo -> CreditLine, $fp);
$fp = str_replace("{BetTypeName}", $_arrBet_Type[$arrLotto["LType"]][$intBT], $fp);
$fp = str_replace("{LottoName}", $arrLotto["Lotto_".$_intLang], $fp);
$fp = str_replace("{GambleIDValue}", $intGID, $fp);
$fp = str_replace("{Bet_TypeValue}", $intBT, $fp);
$fp = str_replace("{LottoIDValue}", $intLID, $fp);
$fp = str_replace("{NumberValue}", $arrGameDate["Number"], $fp);
$fp = str_replace("{UserIDValue}", $objMemberInfo -> ID, $fp);
$fp = str_replace("{WagerConditions}", $strWagerConditions, $fp);

$intMinStakeValue = 9999999;
$intChkMaxPayOff = 9999999;
$intCom_BetLimit = 9999999;
foreach($_arrBetParam[$intBT] as $intAN => $arrBP){
	if($arrApNum[$intAN] === null)continue;

	if(floor($arrBP["MinStake"] / $flMemERate) < $intMinStakeValue)$intMinStakeValue = floor($arrBP["MinStake"] / $flMemERate);
	if(floor($arrBP["MaxPayOff"] / $flMemERate) < $intChkMaxPayOff)$intChkMaxPayOff = floor($arrBP["MaxPayOff"] / $flMemERate);
	if($arrBP["Com_BetLimit"] < $intCom_BetLimit)$intCom_BetLimit = $arrBP["Com_BetLimit"];
}
$fp = str_replace("{MinStake}", (string)$intMinStakeValue, $fp);	//最小下注金額
$fp = str_replace("{MaxPayOff}", $intChkMaxPayOff, $fp);	//最高派彩
$fp = str_replace("{BetLimit}", $objMemberInfo -> arrBetLimit[$intLID][$intBT], $fp);	//單注限額
$fp = str_replace("{EventLimit}", $objMemberInfo -> arrEventLimit[$intLID][$intBT], $fp);	//單號限額
$fp = str_replace("{ShowStakeMoney}", "", $fp);
$fp = str_replace("{ShowWagerContent}", join("@", $arrFrmWagerCon), $fp);
$fp = str_replace("{MaxOdds}", $flMaxOdds, $fp);
$fp = str_replace("{intTotalWNo}", $intTotalWNo, $fp);

//當會員單場限額，大於此樂透單場限額
$chkEventLimit = $objMemberInfo -> arrEventLimit[$intLID][$arrGameDate["Bet_Type"]];
$arrOtherMessage = array();
$_arrLottoLimit[$arrGameDate["Bet_Type"]][0] = floor($_arrLottoLimit[$arrGameDate["Bet_Type"]][0] / $flMemERate);
if($chkEventLimit > $_arrLottoLimit[$arrGameDate["Bet_Type"]][0]){
	$chkEventLimit = $_arrLottoLimit[$arrGameDate["Bet_Type"]][0];
	$arrOtherMessage[0] = $_strWagerCommon["MaxEventLimit"].$chkEventLimit;
}
//當會員單注限額，大於此樂透單注限額
$chkBetLimit = $objMemberInfo -> arrBetLimit[$intLID][$arrGameDate["Bet_Type"]];
$_arrLottoLimit[$arrGameDate["Bet_Type"]][1] = floor($_arrLottoLimit[$arrGameDate["Bet_Type"]][1] / $flMemERate);
if($chkBetLimit > $_arrLottoLimit[$arrGameDate["Bet_Type"]][1]){
	$chkBetLimit = $_arrLottoLimit[$arrGameDate["Bet_Type"]][1];
	$arrOtherMessage[1] = $_strWagerCommon["MaxBetLimit"].$chkBetLimit;
}
//當會員單注限額，大於公司預收單注限額
$CompBetLimit = floor($intCom_BetLimit / ($objMemberInfo -> Layer6Scale*$flMemERate));
if($chkBetLimit > $CompBetLimit){
	$chkBetLimit = $CompBetLimit;
	$arrOtherMessage[1] = $_strWagerCommon["MaxBetLimit"].$chkBetLimit;
}
if(sizeof($arrOtherMessage)){
	$fp = str_replace("{OtherMessage}", join("<br>", $arrOtherMessage), $fp);
}else{
	$fp = str_replace("{OtherMessage}", "", $fp);
}
$fp = str_replace("{chkBetLimit}", $chkBetLimit, $fp);
$fp = str_replace("{chkEventLimit}", $chkEventLimit, $fp);

$fp = str_replace("{UseLimitValue}",$intMaxUsedNumGold, $fp);	//已使用信用額度
$intTmp = $_arrMemStake["_SUM"];
if(empty($intTmp))$intTmp = 0;
$fp = str_replace("{UseCreditValue}",$intTmp, $fp);	//已使用信用額度

$arrCheckKey = split(" ", microtime());
$fp = str_replace("{CheckKeyValue1}", $arrCheckKey[0], $fp);
$fp = str_replace("{CheckKeyValue2}", $arrCheckKey[1], $fp);

if($arrGameDate["isMessage"]){
	$fp = str_replace("{WagerMessage}", $_strWagerCommon[$arrGameDate["Message"]], $fp);
}else{
	$fp = str_replace("{WagerMessage}", "", $fp);
}

while(list($k, $v) = each($_strWagerCommon)){
	$fp = str_replace("{".$k."}", $v, $fp);
}

//輸出
WebOutputHTML($fp, 1);
?>